﻿using A.M.ETO;
using A.M.Storage.Application;
using A.M.Storage.I_Infrastruecure;
using AutoMapper;
using DotNetCore.CAP;
using Newtonsoft.Json;
using Public_Class_Library;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
using W.M.Materiel.Application.DTO;
using W.M.Materiel.Application.IServices;
using W.M.Materiel.Domain.DTO;
using W.M.Materiel.Domain.Entity;
using W.M.Materiel.Infrastruecure.DTO;
using W.M.Materiel.Infrastruecure.IRepository;
using W.M.Materiel.Infrastruecure.Repository;

namespace W.M.Materiel.Application.Services
{
    internal class Product_management_tableServices : BaseServices<product_management_table, string>, IProduct_management_tableServices
    {
        private readonly IProduct_management_tableRepository product_Management_TableRepository;
        private readonly IMultistage_price_tableServices multistage_Price_TableServices;
        private readonly IMapper mapper;
        private readonly Cross_service_Helper cross_Service_Helper;
        private readonly ICapPublisher capPublisher;

        public Product_management_tableServices(IProduct_management_tableRepository product_Management_TableRepository, IMapper mapper, Cross_service_Helper cross_Service_Helper, IMultistage_price_tableServices multistage_Price_TableServices, ICapPublisher capPublisher) : base(product_Management_TableRepository)
        {
            this.product_Management_TableRepository = product_Management_TableRepository;
            this.mapper = mapper;
            this.cross_Service_Helper = cross_Service_Helper;
            this.multistage_Price_TableServices = multistage_Price_TableServices;
            this.capPublisher = capPublisher;
        }

        /// <summary>
        /// 产品信息+分页+查询
        /// </summary>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="name">查询条件：产品名称</param>
        /// <param name="id">查询条件：产品编号</param>
        /// <param name="totalCount"></param>
        /// <param name="pageCount"></param>
        /// <returns></returns>
        public List<Product_managementDTO> GetProductionunit(int pageSize, int pageIndex, string? name, string? id, out int totalCount, out int pageCount)
        {
            return product_Management_TableRepository.GetProductionunit(pageSize, pageIndex, name, id, out totalCount, out pageCount);
        }

        /// <summary>
        /// 添加产品信息
        /// </summary>
        /// <param name="c">产品实体</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiReturn<string>> GetproductAdd(product_management_table c)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            var list = await product_Management_TableRepository.AddInfor_Async(c);
            if (list >0)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "添加成功!";

                //添加成功需要传一个消息队列  传递哪个模块 还有编号
                string numberName = "产品系统-" + c.product_number;
                capPublisher.Publish("GetNewNumber", numberName);
                product_Management_TableRepository.CommitTranInfor();
            }
            else
            {
                apiReturn.Code = 400;
                apiReturn.Message = "添加失败!";
            }
            return apiReturn;
        }

        /// <summary>
        /// 删除产品信息
        /// </summary>
        /// <param name="id">产品编号</param>
        /// <returns>统一返回格式</returns>

        public async Task<ApiReturn<int>> GetproductDel(string? id)
        {
            ApiReturn<int> apiReturn = new ApiReturn<int>();
            var res =await product_Management_TableRepository.GetShowObjAsync(x => x.product_number == id);
            var list =await product_Management_TableRepository.DelInfor_Async(res);
            if (list != null)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "删除成功!";
            }
            else
            {
                apiReturn.Code = 400;
                apiReturn.Message = "删除失败!";
            }
            return apiReturn;
        }

        /// <summary>
        /// 修改产品信息
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public async Task<ApiReturn<string>> GetproductUpdate(product_management_table c)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            var res =await product_Management_TableRepository.GetShowInforAsync(x => x.product_number.Equals(c.product_number));
            foreach (var item in res)
            {
                item.product_number = c.product_number;
                item.product_name=c.product_name;
                item.specification_model = c.specification_model;
                item.brand = c.brand;
                item.unit_id = c.unit_id;
                item.product_classification_id = c.product_classification_id;
                item.product_attribute_id = c.product_attribute_id;
                item.unit_sales_price   = c.unit_sales_price;
                item.minimum_inventory = c.minimum_inventory;
                item.maximum_inventory = c.maximum_inventory;
                item.product_expiration_date = c.product_expiration_date;
                item.alarm_days = c.alarm_days;
                item.status = c.status;
                item.remark = c.remark; 
                item.selling_number = c.selling_number;
                item.packing_requirement = c.packing_requirement;
                item.carton_size = c.carton_size;
                item.packing_method = c.packing_method;
                item.auxiliary_unit = c.auxiliary_unit;
                item.picture = c.picture;
                item.attachments = c.attachments;
            }
            var list =await product_Management_TableRepository.UpdInfor_Batch_Async(res);
            if (list > 0)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "修改成功!";
            }
            else
            {
                apiReturn.Code = 400;
                apiReturn.Message = "修改失败!";
            }
            return apiReturn;
        }


        /// <summary>
        /// 反填产品信息
        /// </summary>
        /// <param name="bom_number">产品编号</param>
        /// <returns>产品数据</returns>
        public async Task<product_management_table> GetBillbackfill(string? product_number)
        {
            Expression<Func<product_management_table, bool>> wherename = x => x.product_number == product_number;
            var list = await product_Management_TableRepository.GetShowObjAsync(wherename);
            return list;
        }


        #region
        /// <summary>
        /// 显示产品信息+分页+查询
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="name">查询条件:产品名称</param>
        /// <param name="id">查询条件:单位id</param>
        /// <returns>统一返回格式</returns>
        //public async Task<PageETO<Product_management_tableDTO>> GetProduct_management(int pageSize, int pageIndex, string? name, int? id)
        //{
        //    PageETO<Product_management_tableDTO> PageETO = new PageETO<Product_management_tableDTO>();

        //    Expression<Func<product_management_table, bool>> wherename = b => true;
        //    if (!string.IsNullOrEmpty(name))
        //    {
        //        wherename = x => x.product_name.Contains(name);
        //    }
        //    if (id != null)
        //    {
        //        wherename = x => x.unit_id == id;
        //    }
        //    var obj = cross_Service_Helper.Get("http://10.31.59.10:5185/api/Table_of_unitsRead/GetTableUrl").Result;
        //    var jsonObj = JsonConvert.DeserializeObject<List<table_of_units>>(obj);
        //    var list = await product_Management_TableRepository.GetShowQueryableAsync(pageIndex, pageSize, wherename);

        //    var res = mapper.Map<List<Product_management_tableDTO>>(list.Data).OrderByDescending(x => x.product_number).ToList();
        //    PageETO.TotalCount = list.TotalCount;
        //    PageETO.PageCount = list.PageCount;
        //    PageETO.Data = res;
        //    #region
        //    //PageETO.TotalCount = list.TotalCount;
        //    //PageETO.PageCount = list.PageCount;
        //    //PageETO.Data = list.Data.Select(x => new Product_management_tableDTO
        //    //{
        //    //    unit_id = x.unit_id,
        //    //    specification_model = x.specification_model,
        //    //    alarm_days = x.alarm_days,
        //    //    attachments = x.attachments,
        //    //    auxiliary_unit = x.auxiliary_unit,
        //    //    brand = x.brand,
        //    //    carton_size = x.carton_size,
        //    //    maximum_inventory = x.maximum_inventory,
        //    //    minimum_inventory = x.minimum_inventory,
        //    //    packing_method = x.packing_method,
        //    //    packing_requirement = x.packing_requirement,
        //    //    picture = x.picture,
        //    //    product_attribute_id = x.product_attribute_id,
        //    //    product_classification_id = x.product_classification_id,
        //    //    product_expiration_date = x.product_expiration_date,
        //    //    product_name = x.product_name,
        //    //    product_number = x.product_number,
        //    //    remark = x.remark,
        //    //    selling_number = x.selling_number,
        //    //    status = x.status,
        //    //    unit_sales_price = x.unit_sales_price,
        //    //}).ToList();
        //    #endregion
        //    return PageETO;
        //}
        #endregion

        //查询生产全部
        public async Task<List<QPromInfor>> GetProductInfor()
        {
            List<product_management_table>? list = await product_Management_TableRepository.GetShowInforAsync(c => c.status);
            var mlist = await multistage_Price_TableServices.GetShowInforAsync();
            var alist = (from a in list
                         join b in mlist
                         on a.selling_number equals b.selling_number
                         select new QPromInfor
                         {
                             product_number = a.product_number,
                             product_name = a.product_name,
                             specification_model = a.specification_model,
                             unit_id = a.unit_id,
                             unit_sales_price = a.unit_sales_price,
                             selling_number = b.selling_number,
                             discount= b.discount,
                         }).ToList();    
          
            return alist;
        }
    }
}
